#include <stdio.h>

#define MAXSIZE	8

int ringbuf[MAXSIZE];
int writeindex = 0;
int readindex = 0;

static int next_data_handle( int index )
{
	return ( index + 1 ) == MAXSIZE ? 0 : ( index + 1 );
}

static void write_buf( int data )
{
	int idx = 0;
	*(ringbuf + writeindex) = data;

	writeindex = next_data_handle( writeindex );

	for ( idx = 0; idx < MAXSIZE; idx++ ) {
		printf( "%4d\n", *(ringbuf + idx ) );
		if ( MAXSIZE - 1 == idx )
			printf( "\n" );
	}

	return ;	
}

static int read_buf()
{
	
	printf( "read data is %d\n", *(ringbuf + readindex) );
	readindex = next_data_handle( readindex );
}

int main( int argc, char **argv )
{
	int data;
	char cmd;

	do {
		printf( "sleect:\nw\t--write\nr\t--read\nt--quit\n" );
		scanf( "%s", &cmd );

		switch ( cmd ) {
		case 'w':
			printf( "Please input data:" );
			scanf( "%d", &data );
			write_buf( data );
			break;

		case 'r':
			data = read_buf();
			break;

		case 'q':
			printf( "quit.\n" );
			break;

		default:
			printf( "Command error.\n" );
			break;
		}
	} while( cmd != 'q' );	
	
	return 0;
}
